package com.example.ui.contact.chat;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import com.example.huashangapp.HSApplication;


/**
 * json消息整合
 * 
 * @author Leone
 * 
 */
public class JsonUtil {
	public static  String USER_ID_KEY = "userid2";//发送方的userid
	public static  String USER_ID_KEY2 = "userid";
	
	public static  String CHANNEL_ID_KEY = "channel_id";
	
	public static  String NICK_KEY = "nickname2";//发送方的昵称
	public static  String NICK_KEY2 = "nick";
	
	public static  String HEAD_ID_KEY = "head_id";
	public static  String TIME_SAMP_KEY = "datetime";
	public static  String MESSAGE_KEY = "message";
	
	
	public static  String TAG_KEY = "tag";
	public static  String PUSH_ID = "pushid";//自己标识
	public static  String PUSH_ID2 = "pushid2";//对方手机标识
	

	public static final String PUSH_ID_KEY = "push_id";

	/**
	 * 创建json消息
	 * @param userId
	 * @param pushId
	 * @param nick
	 * @param message
	 * @return
	 */
	public static String createJsonMsg(String userId, String pushId,
			String nick ,String message) {
		try {

			String result = new JSONStringer().object().key(USER_ID_KEY)
					.value(userId).key(PUSH_ID_KEY).value(pushId)
					.key(NICK_KEY).value(nick).key(MESSAGE_KEY)
					.value(message).toString();
			return result;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 创建json消息
	 * @param userId
	 * @param channelId
	 * @param nick
	 * @param headId
	 * @param timeSamp
	 * @param message
	 * @param tag
	 * @return
	 */
	public static String createJsonMsg(String userId, String channelId,
			String nick, String headId, String timeSamp, String message,
			String tag) {
		try {
			String result = new JSONStringer().object().key(USER_ID_KEY)
					.value(userId).key(CHANNEL_ID_KEY).value(channelId)
					.key(NICK_KEY).value(nick).key(HEAD_ID_KEY).value(headId)
					.key(TIME_SAMP_KEY).value(timeSamp).key(MESSAGE_KEY)
					.value(message).key(TAG_KEY).value(tag).endObject()
					.toString();
			return result;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return "";
	}
	/**
	 * 创建json消息
	 * @param timeSamp
	 * @param message
	 * @param tag
	 * @return
	 */
	public static String createJsonMsg(long timeSamp, String message,
			String tag) {
		SharePreferenceUtil util = HSApplication.getInstance().getSpUtil();
		try {
			String result = new JSONStringer().object().key(USER_ID_KEY)
					.value(util.getUserId()).key(CHANNEL_ID_KEY)
					.value(util.getChannelId()).key(NICK_KEY)
					.value(util.getNick()).key(HEAD_ID_KEY)
					.value(util.getHeadIcon()).key(TIME_SAMP_KEY)
					.value(timeSamp).key(MESSAGE_KEY).value(message)
					.key(TAG_KEY).value(tag).endObject().toString();
			return result;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return "";
	}

	
	/**
	 * 创建json2消息
	 * @param timeSamp
	 * @param message
	 * @param tag
	 * @return
	 */
	public static String createJsonMsg(String datetime ,String message) {
		SharePreferenceUtil util = HSApplication.getInstance().getSpUtil();
		try {
//			String result = new JSONStringer().object().key(PUSH_ID).value(util.getPushId())
//					.key(PUSH_ID2).value(util.getPushId2())
//					.key(USER_ID_KEY).value(util.getUserId())
//					.key(USER_ID_KEY2).value(util.getUserId2())
//					.key(NICK_KEY).value(util.getNick())
//					.key(NICK_KEY2).value(util.getNick2())					
//					.key(MESSAGE_KEY).value(message).endObject().toString();
			String result = new JSONStringer().object().key(PUSH_ID).value("1")
					.key(PUSH_ID2).value("1")
					.key(USER_ID_KEY).value("123")
					.key(USER_ID_KEY2).value("246")
					.key(NICK_KEY).value("leone")
					.key(NICK_KEY2).value("xia")	
					.key(datetime).value(TIME_SAMP_KEY)
					.key(MESSAGE_KEY).value(message).endObject().toString();
			return result;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	
	private static JSONObject getJSONObject(String content) {
		JSONObject jsonContent = null;
		try {
			jsonContent = new JSONObject(content);
		} catch (JSONException e) {
			// e.printStackTrace();
			L.e("Parse bind json infos error: " + e);
		}
		return jsonContent;
	}

	/**
	 * 是不是自己的消息
	 * 
	 * @param msg
	 * @return
	 */
	public static boolean isMe(String msg) {
		boolean isMe = false;
		try {
			JSONObject jsonContent = getJSONObject(msg);
			isMe = jsonContent.getString(USER_ID_KEY).equals(
					HSApplication.getInstance().getSpUtil().getUserId());
		} catch (JSONException e) {
			// e.printStackTrace();
			L.e("Parse bind json infos error: " + e);
		}
		return isMe;
	}

	/**
	 * 从json消息中提取消息内容
	 * 
	 * @param msg
	 * @return
	 */
	public static String getMsgContent(String msg) {
		String message = "";
		try {
			JSONObject jsonContent = new JSONObject(msg);
			message = jsonContent.getString(MESSAGE_KEY);
		} catch (JSONException e) {
			// e.printStackTrace();
			L.e("Parse bind json infos error: " + e);
		}
		return message;
	}
	
	public static String getChannelId(String msg){
		if(!msg.contains("channel_id")){
			msg = "{channel_id:"+msg+"}";
		}
		String channelId = "";
		try {
			JSONObject jsonContent = new JSONObject(msg);
			channelId = jsonContent.getString(CHANNEL_ID_KEY);
		} catch (JSONException e) {
			// e.printStackTrace();
			L.e("Parse bind json infos error: " + e);
		}
		return channelId;
	}
	
	public static String getFromUserId(String msg){

		String userId = "";
		try {
			JSONObject jsonContent = new JSONObject(msg);
			userId = jsonContent.getString(USER_ID_KEY);
		} catch (JSONException e) {
			// e.printStackTrace();
			L.e("Parse bind json infos error: " + e);
		}
		return userId;
	}
	public static String getFromUserId2(String msg){

		String userId = "";
		try {
			JSONObject jsonContent = new JSONObject(msg);
			userId = jsonContent.getString(USER_ID_KEY2);
		} catch (JSONException e) {
			// e.printStackTrace();
			L.e("Parse bind json infos error: " + e);
		}
		return userId;
	}
	
	public static String getFromUserHead(String msg){

		String userHead = "";
		try {
			JSONObject jsonContent = new JSONObject(msg);
			//userHead = jsonContent.getString(HEAD_ID_KEY);
			userHead = "0";
		} catch (JSONException e) {
			// e.printStackTrace();
			L.e("Parse bind json infos error: " + e);
		}
		return userHead;
	}
	
	
	public static String getFromUserNick(String msg){

		String nick = "";
		try {
			JSONObject jsonContent = new JSONObject(msg);
			nick = jsonContent.getString(NICK_KEY);
		} catch (JSONException e) {
			// e.printStackTrace();
			L.e("Parse bind json infos error: " + e);
		}
		return nick;
	}
	
	public static String getFromUserMsg(String msg){
		String Msg = "";
		try {
			JSONObject jsonContent = new JSONObject(msg);
			Msg = jsonContent.getString(MESSAGE_KEY);
		} catch (JSONException e) {
			// e.printStackTrace();
			L.e("Parse bind json infos error: " + e);
		}
		return Msg;		
	}
	
	public static String getFromUserTime(String msg){
		String time = "";
		try {
			JSONObject jsonContent = new JSONObject(msg);
			time = jsonContent.getString(TIME_SAMP_KEY);
		} catch (JSONException e) {
			L.e("Parse bind json infos time error: " + e);
		}
		return time;		
	}
	public static String getTag(String msg){
		String tag = "";
		try {
			JSONObject jsonContent = new JSONObject(msg);
			//tag = jsonContent.getString(TAG_KEY);
		} catch (JSONException e) {
			// e.printStackTrace();
			L.e("Parse bind json infos error: " + e);
		}
		return tag;
	}
	

	
	
	
	/**
	 * 解析地址数据
	 * @param addrStr
	 * @return
	 */
	public static String getAddrStr(String addrStr){
		String tag = "";
		try {
			JSONObject jsonContent = new JSONObject(addrStr);
			tag = jsonContent.getString("addrStr");
		} catch (JSONException e) {
			// e.printStackTrace();
			L.e("Parse bind json getAddrStr error: " + e);
		}
		return tag;
	}
	/**
	 * 解析经度数据
	 * @param latitude
	 * @return
	 */
	public static String getLatitude(String latitude){
		String tag = "";
		try {
			JSONObject jsonContent = new JSONObject(latitude);
			tag = jsonContent.getString("latitude");
		} catch (JSONException e) {
			// e.printStackTrace();
			L.e("Parse bind json getLatitude error: " + e);
		}
		return tag;
	}	
	/**
	 * 解析纬度数据
	 * @param longitude
	 * @return
	 */
	public static String getLongitude(String longitude){
		String tag = "";
		try {
			JSONObject jsonContent = new JSONObject(longitude);
			tag = jsonContent.getString("longitude");
		} catch (JSONException e) {
			// e.printStackTrace();
			L.e("Parse bind json getLongitude error: " + e);
		}
		return tag;
	}		
	

}
